const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

// 从这里复制Cesium静态资源  (Workers、ThirdParty、Assets、Widgets)
const CESIUM_SOURCE_PATH = 'node_modules/cesium/Build/Cesium';
// 打包后Cesium静态资源放置的位置
const CESIUM_BASE_URL = 'public/Cesium/BaseSource';
// 定义一个变量：打包后地图瓦片放置的位置
const CESIUM_MAP_SOURCE_URL = 'public/Cesium/MapSource';

module.exports = {
   mode: 'production',
   entry: './src/index.js', // 入口
   output: {
      filename: 'js/[name].[hash].js',
      path: path.resolve(__dirname, 'dist'), // 打包文件
      clean: true
   },

   // 开发环境（可选）
   devServer: {
      static: './dist',
   },
   // 关闭性能提示
   performance: {
      hints: false,
   },

   plugins: [
      new HtmlWebpackPlugin({
         title: 'output',
         template: './Public/index.html',
      }),

      // 复制静态资源
      new CopyWebpackPlugin({
         patterns: [
            {
               from: path.join(CESIUM_SOURCE_PATH, 'Workers'),
               to: `${CESIUM_BASE_URL}/Workers`,
            },
            {
               from: path.join(CESIUM_SOURCE_PATH, 'ThirdParty'),
               to: `${CESIUM_BASE_URL}/ThirdParty`,
            },
            {
               from: path.join(CESIUM_SOURCE_PATH, 'Assets'),
               to: `${CESIUM_BASE_URL}/Assets`,
            },
            {
               from: path.join(CESIUM_SOURCE_PATH, 'Widgets'),
               to: `${CESIUM_BASE_URL}/Widgets`,
            },
             // 复制地图瓦片到指定位置
            //  {
            //     from: path.join('Public', 'Cesium', 'TmsSource'), 
            //     to: CESIUM_MAP_SOURCE_URL,
            //  },
         ],
      }),

      // 定义全局变量
      new webpack.DefinePlugin({
         CESIUM_BASE_URL: JSON.stringify(CESIUM_BASE_URL), //  CesiumJS 静态资源位置
         CESIUM_MAP_SOURCE_URL: JSON.stringify(CESIUM_MAP_SOURCE_URL) // 地图瓦片资源位置
      }),
   ],

   // 处理 js 、 css 和 图片资源
   module: {
      rules: [
         // js可选
         //   {
         //     test: /\.m?js$/,
         //     exclude: /node_modules/,
         //     use: {
         //        loader: 'babel-loader',
         //        options: {
         //           presets: ['@babel/preset-env'],
         //        },
         //     },
         //  },
         {
            test: /\.css$/i,
            use: ['style-loader', 'css-loader'],
         },
         {
            test: /\.(png|svg|jpg|jpeg|gif)$/i,
            type: 'asset/resource',
         },
      ],
   },
};
